/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.creatjobmanage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.utils.Const;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.InetAddressService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.creatjobmanage.entity.CreatJobMes;
import com.lczyfz.campusinfo.modules.creatjobmanage.service.CreatJobMesService;

/**
 * 创业项目Controller
 * @author zms
 * @version 2018-03-23
 */
@Controller
@RequestMapping(value = "${adminPath}/creatjobmanage/creatJobMes")
public class CreatJobMesController extends BaseController {

	@Autowired
	private CreatJobMesService creatJobMesService;
	@ModelAttribute
	public CreatJobMes get(@RequestParam(required=false) String id) {
		CreatJobMes entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = creatJobMesService.get(id);
		}
		if (entity == null){
			entity = new CreatJobMes();
		}
		return entity;
	}
	
	@RequiresPermissions("creatjobmanage:creatJobMes:view")
	@RequestMapping(value = {"list", ""})
	public String list(CreatJobMes creatJobMes, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CreatJobMes> page = creatJobMesService.findPage(new Page<CreatJobMes>(request, response), creatJobMes); 
		model.addAttribute("page", page);
		return "modules/creatjobmanage/creatJobMesList";
	}

	@RequiresPermissions("creatjobmanage:creatJobMes:view")
	@RequestMapping(value = "form")
	public String form(CreatJobMes creatJobMes, Model model) {
		model.addAttribute("creatJobMes", creatJobMes);
		return "modules/creatjobmanage/creatJobMesForm";
	}

	@RequiresPermissions("creatjobmanage:creatJobMes:edit")
	@RequestMapping(value = "save")
	public String save(CreatJobMes creatJobMes,HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
		if(StringUtils.isNotBlank(creatJobMes.getCreatImg())) {
			creatJobMes.setCreatImg(Const.getinetAddressService(creatJobMes.getCreatImg(),request));
		}
		if (!beanValidator(model, creatJobMes)){
			return form(creatJobMes, model);
		}
		creatJobMesService.save(creatJobMes);
		addMessage(redirectAttributes, "保存创业项目成功");
		return "redirect:"+Global.getAdminPath()+"/creatjobmanage/creatJobMes/?repage";
	}
	
	@RequiresPermissions("creatjobmanage:creatJobMes:edit")
	@RequestMapping(value = "delete")
	public String delete(CreatJobMes creatJobMes, RedirectAttributes redirectAttributes) {
		creatJobMesService.delete(creatJobMes);
		addMessage(redirectAttributes, "删除创业项目成功");
		return "redirect:"+Global.getAdminPath()+"/creatjobmanage/creatJobMes/?repage";
	}

}